<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
// 
//获取商品所属的店铺
function getGoodStore($goods_id){
    $name = M('goods')->where('goods_id='.$goods_id)->getField('goods_store');
    if(empty($name)){
        $name = M('goods')->where('goods_id='.$goods_id)->getField('store_name');
    }
    return $name;
}

//获取库存
function  getStock($goods_attri_model,$goods_id){
    if(empty($goods_attri_model)){
        $stock = M('goods')->where('goods_id='.$goods_id)->getField('goods_stock');
    }else{
        $where['goods_id'] = $goods_id;
        $where['model']  = $goods_attri_model;
        $stock = M('goods_attri_info')->where($where)->getField('stock');
    }
    return $stock;
}

//记录积分获取的日志
function RecordPoints($points,$desc,$user_id,$user_name){
    
    if(empty($user_id)){
        $user_id = session('user_id');
    }
    if(empty($user_name)){
        $user_name = session('user_name');
    }
    $date['user_id'] = $user_id;
    $date['user_name'] = $user_name;
    $date['get_integral'] = $points;
    $date['integral_desc'] = $desc;
    $date['created'] = NOW_TIME;
    M('users_integral_details')->add($date);
}

function get_week($date) {
    //强制转换日期格式
    $date_str = date('Y-m-d', strtotime($date));
    //封装成数组
    $arr = explode("-", $date_str);
    //参数赋值
    //年
    $year = $arr[0];
    //月，输出2位整型，不够2位右对齐
    $month = sprintf('%02d', $arr[1]);
    //日，输出2位整型，不够2位右对齐
    $day = sprintf('%02d', $arr[2]);
    //时分秒默认赋值为0；
    $hour = $minute = $second = 0;
    //转换成时间戳
    $strap = mktime($hour, $minute, $second, $month, $day, $year);
    //获取数字型星期几
    $number_wk = date("w", $strap);
    //自定义星期数组
    $weekArr = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    //获取数字对应的星期
    return $weekArr[$number_wk];
}

//获取商品的真实销量
function addGoodsSale($pay_sn){
    $map['pay_sn'] = $pay_sn;
    $order = M('order')->field('order_id')->where($map)->select();
    foreach ($order as $key => $value) {
        $where['order_id'] = $value['order_id'];
        $goods = M('order_goods')->field('goods_id,goods_num')->where($where)->select();
        foreach ($goods as $k => $v) {
            $maps['goods_id'] = $v['goods_id'];
            M('goods')->where($maps)->setInc('goods_realsale',$v['goods_num']);
        }
    }
}


//获取店铺的商品的总浏览量
function  getStoreHits($store_id){
    $where['store_id'] = $store_id;
    $count = M('goods')->where($where)->sum('goods_hits');
    return $count;
}


function getGroupName($group_id){
    $name = M('role')->where('id='.$group_id)->getField('name');
    return $name;
}


//立即支付URL
function NowPayURL($paySN){
    //{:HOME_URL}{:U('Wxpay/JsApipay',array('id'=>$v['pay_sn']))}
    return  HOME_URL . "/Wxpay/JsApipays?key=" . auth_encode($paySN);
}

function addData(){
    //当天时间
    $time = strtotime(date('Y-m-d'));
    $where = array();
    $where['time'] = $time;
    $info = M('visitor')->where($where)->find();
    if ($info) {
        $where = array();
        $where['id'] = $info['id'];
        M('visitor')->where($where)->setInc('count');
    } else {
        $data = array();
        $data['time'] = $time;
        $data['count'] = 1;
        M('visitor')->add($data);
    }
}

/**
 * 返回节点权限列表(多维数组)
 * @param array $node 节点数据数组
 * @param array $access 权限数据数组
 * @param integer $pid 父级id
 * @return array
 */
function node2layer($node, $access = null, $pid = 0) {
	
	if($node == '') return array();
	$arr = array();

	foreach ($node as $v) {
		if (is_array($access)) {
			
			$v['access'] =in_array($v['id'], $access)? 1 : 0;
		}
		if ($v['pid'] == $pid) {
			$v['child'] = node2layer($node, $access, $v['id']);
			$arr[] =$v;
		}
	}

	return $arr;
}


//快递100免费接口
function kuaidi($com,$num) {
    //$typeCom = 'zhongtong'; //快递公司
    //$typeNu = '424311537568';  //快递单号
    $typeCom = $com; //快递公司
    $typeNu = $num;  //快递单号
    $AppKey = '451fa07b9a6ce50fa817f85cf4bb4263'; //请将XXXXXX替换成您在http://kuaidi100.com/app/reg.html申请到的KEY
    $url = 'http://highapi.kuaidi.com/openapi-querycountordernumber.html?id=' . $AppKey . '&com=' . $typeCom . '&nu=' . $typeNu . '&show=0&muti=0&order=desc';

    //请勿删除变量$powered 的信息，否者本站将不再为你提供快递接口服务。
    //优先使用curl模式发送数据
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    $get_content = curl_exec($curl);
    curl_close($curl);
    //print_r($get_content);
    //p($get_content);
    return $get_content;
}

//快递100免费接口
function kuaidi001($com,$num) {
    //$typeCom = 'zhongtong'; //快递公司
    //$typeNu = '424311537568';  //快递单号
    $typeCom = $com; //快递公司
    $typeNu = $num;  //快递单号
    $AppKey = '451fa07b9a6ce50fa817f85cf4bb4263'; //请将XXXXXX替换成您在http://kuaidi100.com/app/reg.html申请到的KEY
    $url = 'http://highapi.kuaidi.com/openapi-querycountordernumber.html?id=' . $AppKey . '&com=' . $typeCom . '&nu=' . $typeNu . '&show=0&muti=1&order=desc';

    //请勿删除变量$powered 的信息，否者本站将不再为你提供快递接口服务。
    //优先使用curl模式发送数据
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    $get_content = curl_exec($curl);
    curl_close($curl);
    //print_r($get_content);
    //p($get_content);
    return $get_content;
}

/**
 * 系统邮件发送函数
 * @param string $mail_data 邮件信息结构
 * @$mail_data['receiver'] 收件人
 * @$mail_data['subject'] 邮件主题
 * @$mail_data['content']邮件内容
 * @$mail_data['attachment'] 附件列表
 * @return boolean
 */
function sendMail($email, $id, $name = null, $url = null) {
    $site = HOME_URL;
    //生成验证码
    $reg_verify = \Org\Util\String::randString(6, 1);
    session('email_sms',$reg_verify);
    //构造邮件数据
    $mail_data['subject'][0] = C('c_title').' --邮箱密码找回';
    $mail_data['subject'][1] = C('c_title').' --邮箱密码找回';
    $mail_data['subject'][2] = C('c_title').' --信息认证通知';
    $mail_data['subject'][3] = C('c_title').' --信息认证通知';
    $mail_data['subject'][4] = C('c_title').' --信息认证通知';

    $mail_data['content'][0] = "<div><br></div><div><includetail><table width='590' cellspacing='0' cellpadding='0' border='0' align='center'><tbody>"
            . "<tr><td><table width='590' cellspacing='0' cellpadding='0' border='0' align='center'><tbody>"
            . "<tr><td valign='top' style='background:#dc5b3f;height:60px;'><a target='_blank' href={$site}><img style='vertical-align:top;border:none;' height='60' width='168' src='{$site}/Public/img/logo001.png'></a></td></tr>"
            . "<tr><td style='border: 1px solid #E3E3E3; border-top: none;border-bottom:0px;padding:25px;padding-right:0px;padding-bottom:0px;line-height:21px; background-color:#fff;'>"
            . "<p style='font:normal 20px/55px 微软雅黑;color:#f60;margin:0;'>Hi！亲爱的{$name}</p>"
            . "<p style='margin:0;word-wrap:break-word !important; word-spacing:normal !important; padding-top:8px; width:520px; display:block;'>"
            . "<span style='font-size:14px;color:#000;margin:0;'>感谢您使用密码找回服务。<br>请点击链接完成密码重置操作：</span>"
            . "<a target='_blank' href='{$url}' style='font-family:Arial;font-size:11px;word-wrap:break-word !important; word-spacing:normal !important;'>{$url}</a><br>"
            . "<span style='color:#949292;font-size:12px;'>(该链接在20分钟内有效，20分钟后需要重新获取) </span>"
            . "</p><p style='margin:0;padding-top:20px;font-size:12px;'>如果上面不是链接形式，请将地址复制到您的浏览器(例如IE)的地址栏再访问。<br>如果您没有使用密码找回功能，也无须担心，您的帐号还是安全的。您是不是很久没有登陆了啊，请马上去看一看吧： <a href={$site} style='font-family:Arial'>{$site}</a></p>"
            . "<p style='margin:0;padding-top:20px;font-size:12px;'>感谢您的支持，希望您获得愉悦的学习体验。<br>这是一封系统邮件，请勿回复。</p></td>"
            . "</tr><tr><td colspan='3' style=' border-left:1px solid #E3E3E3;width:590px;height:105px;'><img src='{$site}/Public/img/bg100.png' height='105' style='float:right;display:inline;margin-right:-2px;'></td></tr>"
            . "<tr><td style='border:1px solid #E3E3E3; border-top: none;padding:0px 25px;height:3px;font-size:1px;line-height:3px; background-color:#ddd; '>&nbsp;</td></tr></tbody></table>"
            . "</td></tr><tr><td></td></tr></tbody>"
            . "</table></includetail>"
            . "</div>";

    $mail_data['content'][1] = '先生/女士您好：<br>您正使用该邮箱' . $email . '【绑定邮箱/修改邮箱/修改密码】，请在验证码输入框中输入：
    <span style="color:red;font-weight:bold;">' . $reg_verify . '</span>，以完成操作。<br>
    注意：此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作，请及时登录并修改
    密码以保证帐户安全 （工作人员不会向您索取此验证码，请勿泄漏！)';
    $mail_data['content'][2] = '<div><span style="line-height: 1.5;">尊敬的' . $name . ' ，您好。</span></div><div><div>&nbsp; &nbsp;“淘嘟嘟”郑州报业集团旗下网购生活平台提醒：您的留学生专业人才入库储备信息已经提交，进入审核阶段<span style="line-height: 1.5;">。审核结果会邮箱通知您。</span></div><div><br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;留信网留学生专业人才库<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; ' . date('Y-m-d') . '<br></div>';
    $mail_data['content'][3] = '<div><div style="line-height: 21px;"><span style="line-height: 1.5;">尊敬的' . $name . ' ，您好。</span></div><div style="line-height: 21px;"><div>&nbsp; &nbsp;您的河图庄园留学生专业人才库审核通过，入库编号是：' . $bm . '，请登录ku.cscss.com.cn查询；</div><div><br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;留信网留学生专业人才库<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; ' . date('Y-m-d') . '</div>';
    $mail_data['content'][4] = '<div><div style="line-height: 21px;"><span style="line-height: 1.5;">尊敬的' . $name . ' ，您好。</span></div><div style="line-height: 21px;"><div>&nbsp; &nbsp;您的河图庄园留学生专业人才库审核未通过，具体原因；请登录ku.cscss.com.cn查询；</div><div><br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;留信网留学生专业人才库<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; ' . date('Y-m-d') . '</div>';

    //$mail_body_template = $addon_config['default']; //获取邮件模版配置
    //$mail_body = str_replace("[MAILBODY]", $mail_data['content'], $mail_body_template); //使用邮件模版
    $mail_body = $mail_data['content'][$id]; //使用邮件模版
    //$mail = new \Lib\Mailer();
    $mail = new Libs\Mailer\PHPMailer();
    $mail->IsSMTP(); // 使用SMTP方式发送
    $mail->CharSet = 'UTF-8'; // 设置邮件的字符编码
    $mail->Host = 'smtp.qq.com'; // 您的企业邮局服务器
    $mail->Port = 25; // 设置端口
    $mail->SMTPAuth = true; // 启用SMTP验证功能
    $mail->Username = 'service@1hetu.com'; // 邮局用户名(请填写完整的email地址)
    $mail->Password = 'Liguang2016'; // 邮局密码
    $mail->From = 'service@1hetu.com'; //邮件发送者email地址
    $mail->FromName = "“淘嘟嘟”郑州报业集团旗下网购生活平台";
    $mail->AddAddress($email, '“淘嘟嘟”郑州报业集团旗下网购生活平台'); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")

    $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
    $mail->Subject = $mail_data['subject'][$id]; //"PHPMailer测试邮件"; //邮件标题
    $mail->Body = $mail_body; //邮件内容
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        return false;
    } else {
        return true;
    }
}

// 
//去除一些特殊的标签
function cJscript($str) {
    $str = preg_replace("/<\!--.*?-->/si", "", $str); //注释
    $str = preg_replace("/<(\/?meta.*?)>/si", "", $str); //过滤meta标签
    $str = preg_replace("/<(\/?body.*?)>/si", "", $str); //过滤body标签
    $str = preg_replace("/<(\/?link.*?)>/si", "", $str); //过滤link标签
    $str = preg_replace("/<(\/?form.*?)>/si", "", $str); //过滤form标签
    $str = preg_replace("/cookie/si", "COOKIE", $str); //过滤COOKIE标签
    $str = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si", "", $str); //过滤object标签
    $str = preg_replace("/<(\/?objec.*?)>/si", "", $str); //过滤object标签
    $str = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si", "", $str); //过滤noframes标签
    $str = preg_replace("/<(\/?noframes.*?)>/si", "", $str); //过滤noframes标签
    $str = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si", "", $str); //过滤frame标签
    $str = preg_replace("/<(\/?i?frame.*?)>/si", "", $str); //过滤frame标签
    $str = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $str); //过滤script标签
    $str = preg_replace("/<(\/?script.*?)>/si", "", $str); //过滤script标签
    $str = preg_replace("/javascript/si", "Javascript", $str); //过滤script标签
    $str = preg_replace("/vbscript/si", "Vbscript", $str); //过滤script标签
    $str = preg_replace("/on([a-z]+)\s*=/si", "On\\1=", $str); //过滤script标签
    $str = preg_replace("/&#/si", "&＃", $str); //过滤script标签，如javAsCript:alert(
    return $str;
}

/**
 * 自定义字段 封装成 key
 * Model层select()返回的数组
 */
function get_cfg_value() {

    //调取网站信息，数据填充
    $ob = M("config");
    $config = $ob->field('name,data')->where('groupid=0')->select();

    foreach ($config as $key => $value) {
        $toarr[$value['name']] = $value['data'];
    }

    $toarr["THINK_SDK_WEIXIN"] = array(
        'APP_KEY' => getSdkValue('wx_appid'), //应用注册成功后分配的 APP ID
        'APP_SECRET' => getSdkValue('wx_appkey'), //应用注册成功后分配的KEY
        'CALLBACK' => 'weixin',
    );

    $toarr["THINK_SDK_SINA"] = array(
        'APP_KEY' => getSdkValue('sina_appid'), //应用注册成功后分配的 APP ID
        'APP_SECRET' => getSdkValue('sina_appkey'), //应用注册成功后分配的KEY
        'CALLBACK' => 'sina',
    );

    $toarr["THINK_SDK_QQ"] = array(
        'APP_KEY' => getSdkValue('qq_appid'), //应用注册成功后分配的 APP ID
        'APP_SECRET' => getSdkValue('qq_appkey'), //应用注册成功后分配的KEY
        'CALLBACK' => 'qq',
    );
    //微信支付配置
    $toarr["PAY_WEIXIN"] = array(
        'APP_KEY' => getSdkValue('qq_appid'), //应用注册成功后分配的 APP ID
        'APP_SECRET' => getSdkValue('qq_appkey'), //应用注册成功后分配的KEY
    );
    //支付宝支付配置
    $toarr["PAY_ALIPAY"] = array(
        'email' => getSdkValue('alipay_email'), //应用注册成功后分配的 APP ID
        'key' => getSdkValue('alipay_appkey'), //应用注册成功后分配的KEY
        'partner' => getSdkValue('alipay_appid'),
    );
    //支付宝支付配置
    $toarr["PAY_ALIWAPPAY"] = array(
        'email' => getSdkValue('alipay_email'), //应用注册成功后分配的 APP ID
        'key' => getSdkValue('alipay_appkey'), //应用注册成功后分配的KEY
        'partner' => getSdkValue('alipay_appid'),
    );
    //银联支付配置
    $toarr["PAY_UNIONPAY"] = array(
        'key' => getSdkValue('union_appid'), //应用注册成功后分配的 APP ID
        'partner' => getSdkValue('union_appkey'), //应用注册成功后分配的KEY
    );

    return $toarr;
}

function getSdkValue($name) {
    $map['name'] = $name;
    $re = M('config')->where($map)->getField('data');
    return $re;
}

/**
 * 广告位获取信息
 * @author ubten
 * @param $ap_id int 广告分类的ID
 * @param $adv_id, int 广告分类下的广告ID（0则表示该类下的全部分类）
 * @param $adv_type, $int 0为pc 1为手机
 * @param $adv_title 0显示广告标题，1不显示标题
 * @return 返回带链接到广告内容
 */
function getAdver($ap_id, $adv_id = 0, $adv_type = 0, $adv_title = 0) {
    $map['ap_id'] = $ap_id;
    //获取单个广告位
    if ($adv_id != 0) {
        $map['adv_id'] = $adv_id;
        $adv = M('adv')->where($map)->find();
        if ($adv_type == 0) {
            $html = "<div class=\"Advertisement w1090\"><a href=\"" . $adv['adv_pc_url'] . "\" target=\"_blank\"><img src=\"" . $adv['adv_pc_pic'] . "\" /></a></div>";
        } else {
            $html = "<div class=\"box-main\"><div class=\"Abanner\"><a href=\"" . $adv['adv_mb_url'] . "\" target=\"_blank\"><img src=\"" . $adv['adv_mb_pic'] . "\" /></a></div></div>";
        }
    } else {
        $adv = M('adv')->where($map)->order('slide_sort asc')->select();
        if ($adv_title == 0) {
            foreach ($adv as $key=>$value) {
                if ($adv_type == 0) {
                    $html .= "<li style=\"" . $value['buy_style'] . "\" class=\"banner".($key+1)."\"><a href=\"" . $value['adv_pc_url'] . "\" target=\"_blank\"><img src=\"" . $value['adv_pc_pic'] . "\" /></a></li>";
                } else {
                    $html .= "<li><a href=\"" . $value['adv_mb_url'] . "\" target=\"_blank\"><img src=\"" . $value['adv_mb_pic'] . "\" /></a></li>";
                }
            }
        }else{
            foreach ($adv as $value) {
                $html .= "<a href=\"" . $value['adv_pc_url'] . "\"><div class=\"com_goods_align_table\"><div class=\"com_goods_align_td\"><img src=\"" . $value['adv_pc_pic'] . "\"></div></div><p>" . $value['adv_title'] . "</p></a>";
            }
        }
    }
    return $html;
}

/**
 * 功能：生成二维码
 * @param string $qr_url     手机扫描后要跳转的网址
 * @param string $qr_level    默认纠错比例 分为L、M、Q、H四个等级，H代表最高纠错能力
 * @param string $qr_size     二维码图大小，1－10可选，数字越大图片尺寸越大
 * @param string $save_path   图片存储路径
 * @param string $save_prefix 图片名称前缀
 */
function qrcode($qr_url,$type=0) {
    $save_path = __ROOT__ . 'Public/qrcode/';
    $web_path = '/Public/qrcode/';
    $filename = createQRcode($save_path, $qr_url);
    if ($filename) {
        $pic = $web_path . $filename;
    }
    if($type == 0){
        return "<img src='" . $pic . "'>";
    }else{
        return $pic;
    }
}

function createQRcode($save_path, $qr_url = '', $qr_level = 'L', $qr_size = 4, $save_prefix = 'tdd') {
    if (!isset($save_path))
        return '';
    //设置生成png图片的路径
    $PNG_TEMP_DIR = & $save_path;
    //导入二维码核心程序
    $object = new \Libs\Qrcode();
    //检测并创建生成文件夹
    if (!file_exists($PNG_TEMP_DIR)) {
        mkdir($PNG_TEMP_DIR);
    }
    //$filename = $PNG_TEMP_DIR.'test.png';
    $errorCorrectionLevel = 'L';
    if (isset($qr_level) && in_array($qr_level, array('L', 'M', 'Q', 'H'))) {
        $errorCorrectionLevel = & $qr_level;
    }
    $matrixPointSize = 4;
    if (isset($qr_size)) {
        $matrixPointSize = & min(max((int) $qr_size, 1), 10);
    }
    if (isset($qr_url)) {
        if (trim($qr_url) == '') {
            die('data cannot be empty!');
        }
        //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
        $filename = $PNG_TEMP_DIR . $save_prefix . md5($qr_url . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
        //开始生成
        $object->png($qr_url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    } else {
        //默认生成
        $object->png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    if (file_exists($PNG_TEMP_DIR . basename($filename)))
        return basename($filename);
    else
        return FALSE;
}


function array2object($array) {
    if (is_array($array)) {
        $obj = new StdClass();
        foreach ($array as $key => $val) {
            $obj->$key = $val;
        }
    } else {
        $obj = $array;
    }
    return $obj;
}

function object2array($object) {
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    } else {
        $array = $object;
    }
    return $array;
}
